//
//  oldFamilyParentListTableViewController.m
//  laonianbangfamily
//
//  Created by 勇拓 李 on 18/3/26.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldFamilyParentListTableViewController.h"
#import "oldFamilyFirstTableViewCell.h"
#import "oldFamilyFirstAddParentsTableViewCell.h"
#import "oldMyParentsViewController.h"
#import "oldFamilySendOrderViewController.h"
#import "oldFamilyBandParentsViewController.h"
#import "oldActivityAppointmentViewController.h"

@interface oldFamilyParentListTableViewController ()<oldFamilyFirstAddParentsTableViewCellDelegate, oldFamilyFirstTableViewCellDelegate>
@property (nonatomic, copy)NSArray *familyArray;
@end

static NSString *const identifier = @"oldFamilyFirstTableViewCell";
static NSString *const lastIdentifier = @"oldFamilyFirstAddParentsTableViewCell";

@implementation oldFamilyParentListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self refresh];
}

#pragma mark - initView
- (void)initView
{
    [self rigisterCell:identifier];
    [self rigisterCell:lastIdentifier];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的亲属";
}

#pragma mark - initData
- (void)initData
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    [self.tableView.mj_header beginRefreshing];
}

- (void)refresh
{
    WeakSelf
    
    [OLDNetWork httpRequestWithPostPut:URL_GetRelationList params:nil success:^(id data) {
        [weakSelf initDataSource:data];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}

- (void)needReload
{
    [self.tableView.mj_header beginRefreshing];
}

- (void)initDataSource:(id)data
{
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < [data count]; i++) {
        oldServer *old = [[oldServer alloc] initWithDictionary:data[i]];
        [array addObject:old];
    }
    
    _familyArray = array;
}

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_familyArray) {
        return 2;
    }
    
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return [_familyArray count];
            break;
        default:
            return 1;
            break;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:
            return [self configFristSection:tableView indexPath:indexPath];
            break;
        default:
            return [self configLastSection:tableView indexPath:indexPath];;
            break;
    }
}


- (UITableViewCell *)configFristSection:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
{
    oldFamilyFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[oldFamilyFirstTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.server = _familyArray[indexPath.row];
    if (_ifComeFromOrder) {
        cell.ifShowEdit = YES;
        cell.delegate = self;
    }
    
    return cell;
    
}

- (UITableViewCell *)configLastSection:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
{
    oldFamilyFirstAddParentsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:lastIdentifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[oldFamilyFirstAddParentsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:lastIdentifier];
    }
    
    cell.delegate = self;
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 105;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section != 0) {
        return;
    }
    
    if (!_ifComeFromOrder) {
        oldMyParentsViewController *vc = [oldMyParentsViewController new];
        vc.parent = _familyArray[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else
    {
        UIViewController *vc = self.navigationController.viewControllers[[self.navigationController.viewControllers count] - 2];
        
        if ([vc isKindOfClass:[oldFamilySendOrderViewController class]])
        {
            oldFamilySendOrderViewController *oldVc = (oldFamilySendOrderViewController *)vc;
            oldVc.server = _familyArray[indexPath.row];
        }
        else if([vc isKindOfClass:[oldActivityAppointmentViewController class]])
        {
            oldActivityAppointmentViewController *oldVc = (oldActivityAppointmentViewController *)vc;
            oldVc.parent = _familyArray[indexPath.row];
        }
        
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - other
- (void)btnAddTouched
{
    oldFamilyBandParentsViewController *vc = [oldFamilyBandParentsViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)btnEditTapped:(oldServer *)server
{
    oldMyParentsViewController *vc = [oldMyParentsViewController new];
    vc.parent = server;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
